'''
Created on 2011-06-30

@author: Mikael
'''
from PyQt4.QtGui import *
from PyQt4.QtCore import *
from Model.Project import Project
from Model.GenericNode import GenericNode
from Model.TreeModel import TreeModel
from View.Widgets.CircularMenu import CircularMenu
from View.Widgets.CircularMenu import MenuPie
from Controller.ProjectExplorerController import ProjectExplorerController
from View.Widgets.MiniTextEntryWidget import MiniTextEntryWidget
from View.Helpers.FileDialogHelper import FileSystemExplorer
from Model.EditorPath import EditorPath
from Model.NodeResource import NodeResource
from Model.Resource import ResourceType

class ProjectExplorerTreeView(QTreeView):
    
    def __init__(self, CircularMenu):
        QTreeView.__init__(self)
        self.mMenu = CircularMenu
        
        
        
    def mousePressEvent(self, QMouseEvent):
        QTreeView.mousePressEvent(self, QMouseEvent)
        if QMouseEvent.button() == Qt.RightButton:
            self.mMenu.spawn(QCursor.pos().x(), QCursor.pos().y())
            
    
    



class ProjectExplorerWindow(QDockWidget):
    '''
    The resource explorer view
    '''

    def __init__(self, Project, mainWindow):
        '''
        Constructor
        '''
        super(ProjectExplorerWindow, self).__init__("Project")
        
        self.mController = ProjectExplorerController()
        self.mMainWindow = mainWindow
        
        

        
        self.createCircularMenu()
        
        self.mTreeView = ProjectExplorerTreeView(self.mCircularMenu);
        self.mPopupText = MiniTextEntryWidget("Folder1", self)
        self.mPopupText.mSignalReturned.connect(self.folderTextEntered)
        

        self.mTreeView.setModel(Project.mModel)
        self.mProject = Project
        self.setWidget(self.mTreeView)
        self.mProject.mSignalChanged.connect(self.handle_ProjectChanged)
        
        #Drag and drop
        #self.mTreeView.setDragEnabled(1);
        #self.mTreeView.acceptDrops();
        #self.mTreeView.setDropIndicatorShown(1)
        
        self.mTreeView.header().hide();
        
        self.mTreeView.setDragDropMode(QAbstractItemView.InternalMove)

        
        #self.mTreeView.expandAll()
        #self.hbox.addWidget(self.mTreeView)
        
    def folderTextEntered(self):
        folderName = self.mPopupText.getText()
        indices = self.mTreeView.selectedIndexes()
        if len(folderName) > 0:
            self.mController.addFolder(folderName, indices[0])

    def createCircularMenu(self):
        
        # Context menu
        self.mCircularMenu      = CircularMenu(180, 10, self)
        self.mImportResource    = self.mCircularMenu.createTextMenu("Import\nresources...", 12)
        self.mDeleteMenu        = self.mCircularMenu.createTextMenu("Delete", 12)
        self.mCreateFolderMenu  = self.mCircularMenu.createTextMenu("Create\nfolder...", 12)
        self.mOpenMenu          = self.mCircularMenu.createTextMenu("Open", 12)
        self.mCircularMenu.mSignalPressed.connect(self.circularMenuPressed)
        
        # Confirmation menu
        self.mConfMenu = CircularMenu(150, 30, self)
        self.mOkMenu = self.mConfMenu.createTextMenu("Ok", 12)
        self.mCancelMenu = self.mConfMenu.createTextMenu("Cancel", 12)
        self.mConfMenu.mSignalPressed.connect(self.circularMenuPressed)
        
    def circularMenuPressed(self, MenuPie):
        '''
        '''
        if MenuPie == self.mDeleteMenu:
            #indices = self.mTreeView.selectedIndexes()
            #self.mController.deleteNodes(indices)
            self.mConfMenu.spawn(QCursor.pos().x(), QCursor.pos().y())
        elif MenuPie == self.mOkMenu:
            indices = self.mTreeView.selectedIndexes()
            self.mController.deleteNodes(indices)
        elif MenuPie == self.mCreateFolderMenu:
            indices = self.mTreeView.selectedIndexes()
            if len(indices) > 0:
                self.mTreeView.expand(indices[0])
                self.mPopupText.spawn(QCursor.pos().x(), QCursor.pos().y())
        elif MenuPie == self.mImportResource:
            self.mCircularMenu.hide()
            indices = self.mTreeView.selectedIndexes()
            exp = FileSystemExplorer()
            filePath = exp.openOneFile(self, 
                            "Select resources to import", "Material file (*.material);;Mesh files (*.mesh);;Texture files (*.texture)", 
                            EditorPath.mRootPath, 
                            QFileDialog.DontUseNativeDialog)
            self.mController.addResource(filePath, indices[0])
        elif MenuPie == self.mOpenMenu:
            indices = self.mTreeView.selectedIndexes()
            if len(indices) > 0:
                QModelIndex = indices[0]
                node = QModelIndex.internalPointer()
                if isinstance(node, NodeResource):
                    resource = node.mResource
                    if resource.mType == ResourceType.Scene:
                        self.mCircularMenu.hide()
                        self.mMainWindow.enableImage()
                        QMessageBox.information(self,
                            "Loading",
                            "Scene loaded with success")
                        
                    else:
                        self.mCircularMenu.hide()
                        QMessageBox.information(self,
                            "Resource",
                            "You cannot open this resource yet =)")
                
                
            
           # if filePath != "":
                
            #    self.mController.openProject(filePath)
                
                
 
    

    def handle_ProjectChanged(self, GenericNode):
        #self.mTreeView.dataChanged()
        #self.mScene.mModel.updateChange(GenericNode)
        self.mTreeView.setModel(None)
        self.mTreeView.setModel(self.mProject.mModel)
